namespace VfNewMessages
{
    using System;
    using System.Linq;
    using Rsdn.Janus;
    using Rsdn.Janus.CoreServices.Extensibility;
    using Rsdn.Janus.CoreServices.ObjectModel;
    using Rsdn.Janus.Database;
    using Rsdn.Janus.DataModel;
    using Rsdn.SmartApp;

    internal class NewMessages_Message : VirtualForumMessage
    {
        private IForumQueryService forumQueryService;
        private DateTime lastUpdateDateTime;

        public NewMessages_Message(IServiceProvider provider)
            : base(provider)
        {
            forumQueryService = ServiceProvider.GetRequiredService<IForumQueryService>();
        }

        protected override IQueryable<IForumMessage> GetMessages(IJanusDataContext db)
        {
            // don't move to class member we need actual value
            int selfId = base.SharedInfo.GetIntValue(SharedInfoKeys.SelfId) ?? 0;
            lastUpdateDateTime = forumQueryService.LastUpdateDate;

            return db.Messages(m => m.Date > lastUpdateDateTime && m.UserID != selfId);
            #region SQL
            /*            
            --  SQLite
            -- DECLARE @lastUpdateDateTime DateTime
            -- DECLARE @p1 Int32

            -- SET @lastUpdateDateTime = 01.06.2011 20:48:40
            -- SET @p1 = 9

            SELECT
	            [m].[mid] as [ID],
	            [m].[tid] as [TopicID],
	            [m].[pid] as [ParentID],
	            [m].[gid] as [ForumID],
	            [m].[Subject],
	            [m].[dte] as [Date1],
	            [m].[uid] as [UserID],
	            [m].[uclass] as [UserClass],
	            [m].[UserNick],
	            [m].[Name],
	            [m].[article_id] as [ArticleId],
	            [m].[LastModerated],
	            [m].[Closed],
	            [m].[Message],
	            [m].[IsMarked],
	            [m].[IsRead],
	            [m].[ReadReplies]
            FROM
	            [messages] [m]
            WHERE
	            [m].[dte] > @lastUpdateDateTime AND [m].[uid] <> @p1

            */
            #endregion
        }
        
        protected override bool IsLoadAllMessages
        {
            get { return true; }
        }
    }
}